package com.bookshop.ui.marketManager;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.bookshop.logic.RemoteServiceAdapter;
import com.bookshop.ui.PageJumper;
import com.bookshop.ui.component.ComponentFactory;
import com.bookshop.ui.component.TurnablePagePanel;

public class OrderManagePage extends TurnablePagePanel<OrderManagePage.InvolvedData>{
	protected static final InvolvedData[] ZERO_INVOLVED_DATAS = new InvolvedData[0];

	public static class InvolvedData {
		public String mOrderId;
		public String mDate;
		public String mPrice;
		public String mState;
		public String[] mBook;
		public String mName;
		public String mAddress;
		public String mPhone;
	}

	private JPanel mInnerPanel;
	
	public OrderManagePage(PageJumper pJumper) {
		super(pJumper);
		init();
	}
	
	private void init() {
		mInnerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
		mInnerPanel.setBackground(new Color(255, 255, 255));

		InvolvedData[] datas = getData();
		loadNewPage(datas);
		
		JScrollPane scrollPane = ComponentFactory.createScrollPane(mInnerPanel);
		scrollPane.setBounds(81, 117, 613, 360);
		add(scrollPane);
		
		mTurnLabels[0].setBounds(340, 500, 47, 16);
		add(mTurnLabels[0]);
		
		mTurnLabels[1].setBounds(440, 500, 47, 16);
		add(mTurnLabels[1]);
	}

	@Override
	protected InvolvedData[] getData() {
		RemoteServiceAdapter adapter = RemoteServiceAdapter.instance();
		InvolvedData[] datas = adapter.showCustomerOrders(mCurrentPage);
		
		if(datas == null) return ZERO_INVOLVED_DATAS;
		return datas;
	}

	@Override
	protected void loadNewPage(InvolvedData[] pDatas) {
		mInnerPanel.removeAll();
		mInnerPanel.validate();
		mInnerPanel.repaint();
		if (pDatas == null) return;
		int size = pDatas.length;
		mInnerPanel.setPreferredSize(new Dimension(613, OrderItemPanel.sHeight * size));
		
		for (int i = 0; i < size; i++) {
			InvolvedData data = pDatas[i];
			OrderItemPanel orderItemPanal = new OrderItemPanel(data.mOrderId, data.mDate, data.mPrice, data.mState,
															   data.mBook, data.mName, data.mAddress, data.mPhone);
			mInnerPanel.add(orderItemPanal);
		}
		
		mInnerPanel.validate();
		mInnerPanel.repaint();
	}
}
